local chonge = fk.CreateSkill{
    name = "SP_heg__chonge",
    tags = { Skill.DeputyPlace },
}

Fk:loadTranslationTable{
    ["SP_heg__chonge"] = "冲轭",
    [":SP_heg__chonge"] = "副将技，与你势力相同的其他角色造成伤害时，若你处于围攻关系，你可以将一张牌交给其，然后弃置围攻关系中一名角色的一张牌。",

    ["#SP_heg__chonge_choose"] = "冲轭：选择一张牌，交给 %src",
    ["#SP_heg__chonge_chooseplayer"] = "冲轭：选择围攻关系的一名角色，弃置其的一张牌",
    ["#SP_heg__chonge_discard"] = "冲轭：选择 %src 的一张牌弃置",

    ["$SP_heg__chonge1"]= "守护之獒，虽无龙鳞虎爪，亦可保家国太平。",
    ["$SP_heg__chonge2"] = "吴蜀得龙虎犹难兴，大魏得獒犬而功成。",
}

local H = require "packages.ol_hegemony.util"

chonge:addEffect(fk.DamageCaused,{
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    local next = player:getNextAlive()
    local last = player:getLastAlive()
    return target ~= player and player:hasSkill(chonge.name) and H.compareKingdomWith(target, player)
    and (H.inSiegeRelation(player, next:getNextAlive(), next) or H.inSiegeRelation(player, last:getLastAlive(), last)
    or H.inSiegeRelation(last, next, player))
  end,
  on_cost = function (self, event, target, player, data)
    local next = player:getNextAlive()
    local last = player:getLastAlive()
    local targets = {}
    if H.inSiegeRelation(player, next:getNextAlive(), next) then
      table.insert(targets, player)
      table.insert(targets, next)
      table.insert(targets, next:getNextAlive())
    end
    if H.inSiegeRelation(player, last:getLastAlive(), last) then
      table.insert(targets, player)
      table.insert(targets, last:getLastAlive())
      table.insert(targets, last)
    end
    if H.inSiegeRelation(last, next, player) then
      table.insert(targets, last)
      table.insert(targets, next)
      table.insert(targets, player)
    end
    if #targets > 0 then
    local targets2 = {}
    for _, p in ipairs(targets) do
      if not p:isNude() then
        table.insert(targets2, p)
      end
    end
    if #targets2 > 0 then
      local cards = player.room:askToCards(player,{
        min_num = 1,
        max_num = 1,
        skill_name = chonge.name,
        pattern = ".|.|.|hand,equip|.|.|.",
        prompt = "#SP_heg__chonge_choose:"..target.id,
        cancelable = true,
      })
      if  #cards > 0 then
        event:setCostData(self, { cards = cards })
        return true
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local c = event:getCostData(self).cards[1]
    room:moveCardTo(c, Player.Hand, target, fk.ReasonGive, chonge.name, nil, false, player)
     local next = player:getNextAlive()
    local last = player:getLastAlive()
    local targets = {}
    local targets2 = {}
    if H.inSiegeRelation(player, next:getNextAlive(), next) then
      table.insert(targets, player)
      table.insert(targets, next)
      table.insert(targets, next:getNextAlive())
    end
    if H.inSiegeRelation(player, last:getLastAlive(), last) then
      table.insert(targets, player)
      table.insert(targets, last:getLastAlive())
      table.insert(targets, last)
    end
    if H.inSiegeRelation(last, next, player) then
      table.insert(targets, last)
      table.insert(targets, next)
      table.insert(targets, player)
    end
    if #targets > 0 then
    for _, p in ipairs(targets) do
      if not p:isNude() then
        table.insert(targets2, p)
      end
    end
  end
  if #targets2 == 0 then return end
  local tos = room:askToChoosePlayers(player,{
    min_num = 1,
    max_num = 1,
    targets = targets2,
    prompt = "#SP_heg__chonge_chooseplayer",
    skill_name = chonge.name,
    cancelable = false,
  })
  if #tos > 0 then
    local card = room:askToChooseCard(player,{
      target = tos[1],
      skill_name = chonge.name,
      prompt = "#SP_heg__chonge_discard:"..tos[1].id,
      flag = "he",
    })
    if card then
      room:throwCard(card, chonge.name, tos[1], player)
    end
  end
  end,
})

return chonge